/**
 * project6
 *
 * brief description of the program
 *
 * This program is released to public domain according to
 * author's wish, you can view full license at
 * http://tomatoeskit.org/upload/PublicDomainLicense.txt
 *
 * @author Fangning CHENG (Alpha.L Cheng) <chengf@purdue.edu>
 *
 * @lab section number 
 *
 * @date Oct 24, 2013
 *
 */

import java.util.Scanner;
import edu.purdue.cs.cs180.channel.*;


public class Requester implements MessageListener {
    Channel channel;
    Scanner scanner = new Scanner(System.in);

    Requester(Channel cnl)
    {
        channel = cnl;
        channel.setMessageListener(this);
        processPrompt();
    }

    @Override
    public void messageReceived(String message, int clientID) {
        String msg[] = message.split("\\s+");
        if (msg.length < 2)
            return;
        if (msg[0].equals("VOLUNTEER")) {
            try {
                System.out.println(String.format("Volunteer %d assigned and will arrive shortly.", 
                        Integer.parseInt(msg[1])));
            } catch (NumberFormatException e) {
                System.out.println("Hey! This line should never be printed!");
                System.out.println("If you see this line, check your volunteer implementation.");
                System.out.println("It usually indicates your VOLUNTEER ID is incorrect -- not an integer");
                System.out.println("(* - -)");
            }
            processPrompt();
        }
    }

    public void sendRequest(String destination)
    {
        try {
            channel.sendMessage("REQUEST " + destination);
            System.out.println("Waiting for volunteer...");
        } catch (ChannelException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

    private void processPrompt()
    {       
        do {   
            printPrompt();

            String input = scanner.nextLine();
            int opt = 0;
            String destination = "";
            try {
                opt = Integer.parseInt(input);
            } catch (NumberFormatException e) {
                System.out.println("Invalid number format. Please try again.");
                continue;
            }
            switch (opt) {
                case 1:
                    destination = "CL50";
                    break;
                case 2:
                    destination = "EE";
                    break;
                case 3:
                    destination = "LWSN";
                    break;
                case 4:
                    destination = "PMU";
                    break;
                case 5:
                    destination = "PUSH";
                    break;
                default:
                    System.out.println("Invalid input. Please try again.");
                    continue;
            }
            sendRequest(destination);
            break;  // break is here. It's not while (true).
        } while (true);
    }

    private void printPrompt() {
        System.out.println("1. CL50 - Class of 1950 Lecture Hall");
        System.out.println("2. EE - Electrical Engineering Building");
        System.out.println("3. LWSN - Lawson Computer Science Building");
        System.out.println("4. PMU - Purdue Memorial Union");
        System.out.println("5. PUSH - Purdue University Student Health Center");
        System.out.print("Enter your location (1-5): ");
    }

    public static void main(String[] args) 
    {
        // ------------------------SET UP NET ENVIRONMENT---------------------------------------
        int argsLen = args.length;
        String srvAddr;
        int srvPort;
        if (argsLen < 2) {
            System.out.println("Invalid arguments");
            return;
        }
        try {
            srvPort = Integer.parseInt(args[1]);
            if (srvPort < 1024 || srvPort > 65535)
                throw new Exception();
        } catch (Exception ex) {
            System.out.println("Invalid port");
            return;
        }
        srvAddr = args[0];
        @SuppressWarnings("unused")
        Requester requester;
        try {
            requester = new Requester(new TCPChannel(srvAddr, srvPort));
        } catch (ChannelException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        // ------------------------END SET UP NET ENVIRONMENT-------------------------------------
    }
}
